/*
 * @(#)SwingContentExtractor.java	0.4 12/28/04
 *
 * Copyright 2004 Fabio Gasparetti. All rights reserved.
 */

package org.tabularium.text.html;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;

import javax.swing.text.html.*;
import javax.swing.text.html.HTMLEditorKit.*;
import javax.swing.text.html.parser.*;

/**
 * Extracts content form Web pages exploiting the Swing internal parser.
 * 
 * Note: The Swing text extractors does not filter out Java script code.
 *
 * @author  Fabio Gasparetti
 * @version 0.4, 12/28/04
 */

public class SwingContentExtractor extends ContentExtractor {
    /**
     * Extracts the text from a html page by means of the Swing parser.
     */
    public String extractText(String url, String htmltext) throws Exception {
      //String charset = new String("ISO-8859-1");

      // setup the swing parser callback
      TextParserCallback callback = new TextParserCallback();
      // parse
      ParserDelegator parserDelegator = new ParserDelegator();
      StringReader reader = new StringReader(htmltext);
      parserDelegator.parse(reader, callback, true);
      return callback.getExtractedText();
    }


    // for debugging
    public static void main(String[] args) {
      try {
        InputStream is = new FileInputStream("prova.html");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        String line;
        StringBuffer doc = new StringBuffer();
        while ( (line = reader.readLine()) != null) {
          doc.append(line + "\n");
        }
        is.close();
        SwingContentExtractor ce = new SwingContentExtractor();
        System.out.println(ce.extractText(".", doc.toString()));
      }
      catch (Exception ex) {
          System.out.println(ex.toString());
          ex.printStackTrace();
      }
    }
  }

  /*
   * PARSER VISITOR/CALLBACK CLASSES
   */
  class TextParserCallback extends HTMLEditorKit.ParserCallback {

    protected StringBuffer text = new StringBuffer();

    public TextParserCallback() {
      super();
    }

    public String getExtractedText() {
      return text.toString();
    }

    public void handleText(char[] data, int pos) {
      text.append(data);
      text.append(' ');
    }
  }

